﻿using System;
using qcloudsms_csharp;
using qcloudsms_csharp.httpclient;
using qcloudsms_csharp.json;
using System.Threading.Tasks;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms.V20190711;
using TencentCloud.Sms.V20190711.Models;
using com.uplus.log4net;

namespace com.uplus.common
{
    public static class TecentCloudSMS
    {
        #region
        /// <summary>
        /// 根据模板ID向一个手机号发送短信
        /// </summary>
        /// <param name="appId">config里的appId</param>
        /// <param name="appKey">config里的appKey</param>
        /// <param name="phoneNumber">手机号码</param>
        /// <param name="templateId">模板ID</param>
        /// <param name="msg">验证码内容</param>
        /// <param name="time">过期时间</param>
        /// <param name="errorMsg">错误原因</param>
        /// <returns>0：正常，1：JSONException异常，2：HTTPException异常，3：其他异常</returns>
        //public static int SendMsg(int appId, string appKey, string phoneNumber, int templateId, string msg, int time, out string errorMsg)
        //{
        //    int res = 0;
        //    string error = "";
        //    try
        //    {
        //        SmsSingleSender ssender = new SmsSingleSender(appId, appKey);
        //        var result = ssender.sendWithParam("86", phoneNumber, templateId, new[] { msg, time.ToString() }, "", "", "");
        //    }
        //    catch (JSONException e)
        //    {
        //        res = 1;
        //        error = e.ToString();
        //    }
        //    catch (HTTPException e)
        //    {
        //        res = 2;
        //        error = e.ToString();
        //    }
        //    catch (Exception e)
        //    {
        //        res = 3;
        //        error = e.ToString();
        //    }
        //    errorMsg = error;
        //    return res;
        //}
        #endregion

       
        public static int SendMsg(string sdkAppid, string appId, string appKey, string sign, string[] phoneNumberSet, string templateID, string[] templateParamSet)
        {
            Log4Helper.Info("", $"发送短信正常开始！入参： sdkAppid: {sdkAppid}  appId: {appId} appKey: {appKey} sign: {sign} phoneNumberSet: {phoneNumberSet} templateID: {templateID} templateParamSet: {templateParamSet}");
            int res = 0;
            try
            {
                Credential cred = new Credential
                {
                    SecretId = appId,
                    SecretKey = appKey
                };

                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = "sms.tencentcloudapi.com";
                clientProfile.HttpProfile = httpProfile;

                SmsClient client = new SmsClient(cred, "", clientProfile);
                SendSmsRequest req = new SendSmsRequest();

                //req.SmsSdkAppid = "1400308559";//1400308559
                //req.Sign = "浦诚环卫";
                //req.PhoneNumberSet = new String[] { "+8619952311985" };
                //req.TemplateID = "191185";
                //req.TemplateParamSet = new String[] { "55", "78" };

                req.SmsSdkAppid = sdkAppid;
                req.Sign = sign;
                req.PhoneNumberSet = phoneNumberSet;
                req.TemplateID = templateID;
                req.TemplateParamSet = templateParamSet;

                SendSmsResponse resp = client.SendSmsSync(req);

                // 输出 JSON 格式的字符串回包
                Console.WriteLine(AbstractModel.ToJsonString(resp));
                Log4Helper.Info("", $"发送短信正常结束！返回值：{AbstractModel.ToJsonString(resp)}");
            }
            catch (Exception e)
            {
                Log4Helper.Error("", "发送短信失败！", e); 
            }
            return 1;
        }

        public static void SendMsg(string appId, string appkey)
        {
            int res = 0;
            

                Credential cred = new Credential
                {
                    SecretId = appId,
                    SecretKey = appkey
                };

                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = "sms.tencentcloudapi.com";
                clientProfile.HttpProfile = httpProfile;

                SmsClient client = new SmsClient(cred, "", clientProfile);
                SendSmsRequest req = new SendSmsRequest();

                req.SmsSdkAppid = "1400308559";//1400308559
                req.Sign = "浦诚环卫";
                //req.PhoneNumberSet = new String[] { "+8619952311985"};
                req.PhoneNumberSet = new String[] { "+8619952311985" };
                //req.PhoneNumberSet = phoneNumberSet;
                req.TemplateID = "191185";
                //req.TemplateID = templateID;
                
                req.TemplateParamSet = new String[] { "55", "78" };
                //req.TemplateParamSet = templateParamSet;
               
                SendSmsResponse resp = client.SendSmsSync(req);

                // 输出 JSON 格式的字符串回包
                Console.WriteLine(AbstractModel.ToJsonString(resp));
            
        }
    }
}
